package com.leyou.search.mq;

import com.leyou.search.service.SearchService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static com.leyou.common.mq.MQConstants.Exchange.ITEM_EXCHANGE_NAME;
import static com.leyou.common.mq.MQConstants.Queue.SEARCH_ITEM_DOWN;
import static com.leyou.common.mq.MQConstants.Queue.SEARCH_ITEM_UP;
import static com.leyou.common.mq.MQConstants.RoutingKey.ITEM_DOWN_KEY;
import static com.leyou.common.mq.MQConstants.RoutingKey.ITEM_UP_KEY;

/**
 * 消费消息
 */
@Component
public class ItemListener {

    @Autowired
    private SearchService searchService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = SEARCH_ITEM_UP,durable = "true"),      //商品服务上架对列
            exchange = @Exchange(
                    name = ITEM_EXCHANGE_NAME,type = ExchangeTypes.TOPIC),     //商品服务交换机  通配符订阅模式
            key = ITEM_UP_KEY                                                    //RoutingKey

    ))
    public void listenInsert(Long spuId){
        if (spuId != null){
            searchService.createIndex(spuId);
        }
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = SEARCH_ITEM_DOWN,durable = "true"),
            exchange = @Exchange(
                    name = ITEM_EXCHANGE_NAME,type = ExchangeTypes.TOPIC),
            key = ITEM_DOWN_KEY

    ))
    public void listenDelete(Long spuId){
        if (spuId != null){
            searchService.deleteIndex(spuId);
        }
    }
}
